package com.metis.document.parse.dialog.pipeline.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

import com.metis.document.parse.dialog.pipeline.TextPostProcess;
import com.metis.document.parse.dialog.pipeline.TitleTextPostProcess;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class ChaosCharFilter implements TextPostProcess, TitleTextPostProcess {

    private final Set<String> writeList = new HashSet<>();
    {
        Resource resource = new ClassPathResource("char_list.txt");
        try (InputStream is = resource.getInputStream()){
            Scanner scanner = new Scanner(is);
            while (scanner.hasNextLine()) {
                writeList.add(scanner.nextLine());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public String process(@NotNull String input) {
        StringBuilder sb = new StringBuilder();
        for (char c : input.toCharArray()) {
            if (writeList.contains(String.valueOf(c))) {
                sb.append(c);
            }

        }
        return sb.toString();
    }
}
